package com.schedule.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.schedule.dao.BusDao;
import com.schedule.dao.PlaceDao;
import com.schedule.dao.ScheduleDao;
import com.schedule.model.Bus;
import com.schedule.model.Place;
import com.schedule.model.Schedule;


@Controller
@RequestMapping("/schedule")
public class ScheduleController {
	@Autowired
	private ScheduleDao scheduleDao;
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView getAll(
			HttpSession session
			){
		ModelAndView mav=new ModelAndView("schedule/scheduleList");
		List<Schedule> schedules=scheduleDao.findAll();
		System.out.println(schedules.size());
		mav.addObject("schedules",schedules);
		 String adminName=session.getAttribute("adminName").toString(); 
		 mav.addObject("name",adminName);
		return mav;
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public ModelAndView delete(
			@RequestParam String busId
			){
			scheduleDao.deleteSchedule(busId);
		ModelAndView mav=new ModelAndView("redirect:/schedule");
		return mav;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.GET)
	public ModelAndView update(
			@RequestParam String busId
			){
		Schedule schedule=scheduleDao.findById(busId);
		ModelAndView mav=new ModelAndView("schedule/updateSchedule");
		List<String> places=scheduleDao.AllPlace();
		mav.addObject("places",places);
		mav.addObject(schedule);
		return mav;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public ModelAndView update(
			@RequestParam String busId,
			@RequestParam String starting,
			@RequestParam String ending,
			@RequestParam String time
			){
		Schedule schedule=new Schedule();
		schedule.setBusId(busId);
		schedule.setStarting(starting);
		schedule.setEnding(ending);
		schedule.setTime(time);
		scheduleDao.updateSchedule(schedule);
		ModelAndView mav=new ModelAndView("redirect:/schedule");
		return mav;
	}
	
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public ModelAndView add(){
		List<String> places=scheduleDao.AllPlace();
		List<String> buses=scheduleDao.AllBus();
		ModelAndView mav=new ModelAndView("schedule/addSchedule");
		mav.addObject("buses",buses);
		mav.addObject("places",places);
		return mav;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ModelAndView add(
			@RequestParam String busId,
			@RequestParam String starting,
			@RequestParam String ending,
			@RequestParam String time
			){
		Schedule schedule=new Schedule();
		schedule.setBusId(busId);
		schedule.setStarting(starting);
		schedule.setEnding(ending);
		schedule.setTime(time);
		scheduleDao.addSchedule(schedule);
		ModelAndView mav=new ModelAndView("redirect:/schedule");
		return mav;
	}
}
